/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpe.cin.dsoa.visualvm.facade;


import br.ufpe.cin.dsoa.qos.management.ManagementAgentMXBean;
import br.ufpe.cin.dsoa.visualvm.persistence.CategoryDAO;
import br.ufpe.cin.dsoa.visualvm.persistence.CharacteristicDAO;
import br.ufpe.cin.dsoa.visualvm.domain.QoSCategory;
import com.sun.tools.visualvm.application.Application;
import com.sun.tools.visualvm.tools.jmx.JmxModelFactory;
import java.util.Date;
import java.util.Iterator;
import javax.management.JMX;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.openide.util.Exceptions;

/**
 *
 * @author rafael.lima
 */
public class PrincipalFacade {

    private CharacteristicDAO characteristic;
    private CategoryDAO category;
    private ManagementAgentMXBean mbean = null;
    String mbeanName = "management:type=Agent";

    public PrincipalFacade(Application app) {

        
        try {
        this.mbean = JMX.newMXBeanProxy(JmxModelFactory.getJmxModelFor(app).getMBeanServerConnection(), new ObjectName(mbeanName), ManagementAgentMXBean.class);
        } catch (MalformedObjectNameException ex) {
        Exceptions.printStackTrace(ex);
        } catch (NullPointerException ex) {
        Exceptions.printStackTrace(ex);
        }

        this.characteristic = new CharacteristicDAO();
        this.category = new CategoryDAO();
    }

    public void addQoSCategory(QoSCategory category) {

        this.category.add(category);
    }

    public void removeQoSCategory(String string) {

        this.category.remove(string);

    }

    public void updateQoSCategory(String selectedCategory, QoSCategory newCategory) {

        this.category.update(selectedCategory, newCategory);

    }

   public QoSCategory getQoSCategory(String string) {


        return this.category.getCategory(string);
    }
   
   public String[] getAvailableQoSCategories(){
       
       String[] ret = new String[this.category.getAllCategories().size()];
       
       Iterator<QoSCategory> it = this.category.getAllCategories().iterator();
       int i = 0;
       
       while(it.hasNext())
           ret[i++] = it.next().getID();
       
       
       return ret;
   }

   

    public String[] getAvailableServices() {
        
        //TODO uncoment
        return mbean.getServices();
        
        //return new String[]{"Service 1","Service 2"};

    }

    public String[] getAvailableHandlers() {
        
        

        //TODO uncoment
        return mbean.getHandlers();
        
       // return new String[]{"Hand1","Handler2"};
    }

   
    public String[] getAvailableOperationFor(String service) {

        //TODO uncoment
        return mbean.getOperations(service);
       // return new String[]{service.intern(),service.toUpperCase(),service.toLowerCase()};
    }

  
    public void createBind(String toString, String toString0, String iD, String iD0, String statistic, String unit, String toString1, int parseInt, String string, Date value, Date value0, String[] hand) {
        //TODO uncoment
        this.mbean.createMonitoringConfiguration(toString, toString0, iD, iD0, statistic, unit, toString1, parseInt, string, value, value0, hand);
    }

   
}
